/*
 * ParameterDialog.java
 */

package xq.ide.forms;

import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author  ThorntonRP
 */
public class ParameterDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = -2165974774297772256L;

	public static final int APPROVE_OPTION = 1;
	public static final int CANCEL_OPTION  = 2;

	private final List<JTextField> fields = new ArrayList<JTextField>();
	private Map<String, String> parameters;
	private int option;

    public ParameterDialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, modal);
        initComponents();
		setTitle(title);
		setLocationRelativeTo(null);
    }

	public void setParameters(Map<String, String> parameters) {
		this.parameters = parameters;
		fields.clear();
		fieldPanel.removeAll();
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets.top = 5;
		gbc.insets.left = 5;
		gbc.insets.bottom = 5;
		gbc.insets.right = 5;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.EAST;
		for (Map.Entry<String, String> entry : parameters.entrySet()) {
			String name = entry.getKey();
			String value = entry.getValue();
			JLabel nameLabel = new JLabel(name);
			JTextField valueTextField = new JTextField(value);
			valueTextField.setName(name);
			if (gbc.gridy == 0) {
				valueTextField.requestFocusInWindow();
			}
			gbc.weightx = 0.0;
			gbc.fill = GridBagConstraints.NONE;
			fieldPanel.add(nameLabel, gbc);
			gbc.weightx = 1.0;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			fieldPanel.add(valueTextField, gbc);
			gbc.gridy++;
			fields.add(valueTextField);
		}
		pack();
	}

	public Map<String, String> getParameters() {
		return parameters;
	}

	public int getOption() {
		return option;
	}

	@Override
	public void setVisible(boolean b) {
		option = 0;
		super.setVisible(b);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldPanel = new javax.swing.JPanel();
        javax.swing.JPanel buttonPanel = new javax.swing.JPanel();
        javax.swing.JButton continueButton = new javax.swing.JButton();
        getRootPane().setDefaultButton(continueButton);
        javax.swing.JButton cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("null");

        fieldPanel.setLayout(new java.awt.GridBagLayout());
        getContentPane().add(fieldPanel, java.awt.BorderLayout.CENTER);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("xq/ide/forms/Bundle"); // NOI18N
        continueButton.setText(bundle.getString("ParameterDialog.continueButton.text")); // NOI18N
        continueButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                continueButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(bundle.getString("ParameterDialog.cancelButton.text")); // NOI18N
        cancelButton.setDefaultCapable(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap(234, Short.MAX_VALUE)
                .addComponent(continueButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap())
        );

        buttonPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, continueButton});

        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(continueButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void continueButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continueButtonActionPerformed
	option = APPROVE_OPTION;
	for (JTextField field : fields) {
		parameters.put(field.getName(), field.getText());
	}
	super.setVisible(false);
}//GEN-LAST:event_continueButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
	option = CANCEL_OPTION;
	super.setVisible(false);
}//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JPanel fieldPanel;
    // End of variables declaration//GEN-END:variables

}
